package xxy.com.customview.view;

import android.content.Context;
import androidx.annotation.NonNull;
import androidx.coordinatorlayout.widget.CoordinatorLayout;
import androidx.core.view.ViewCompat;
import androidx.core.widget.NestedScrollView;
import android.util.AttributeSet;
import android.util.Log;
import android.view.View;

/**
 * Created by Administrator on 2017/12/27.
 */

public class NestScrollViewBehavior1 extends CoordinatorLayout.Behavior {

    public NestScrollViewBehavior1(Context context, AttributeSet attrs) {
        super(context, attrs);
    }

    /**
     * 判断是否进行嵌套滑动，是的话就接受后续事件
     * @param coordinatorLayout
     * @param child
     * @param directTargetChild
     * @param target
     * @param axes
     * @param type
     * @return
     */
    @Override
    public boolean onStartNestedScroll(@NonNull CoordinatorLayout coordinatorLayout, @NonNull View child, @NonNull View directTargetChild, @NonNull View target, int axes, int type) {
        Log.e("NestScrollViewBehavior1", (axes == ViewCompat.SCROLL_AXIS_VERTICAL) + "");
        return axes == ViewCompat.SCROLL_AXIS_VERTICAL;
    }

    /**
     * 相比onNestedScroll 只是比他在生命周期中提前被调用
     * @param coordinatorLayout
     * @param child
     * @param target
     * @param dx
     * @param dy
     * @param consumed
     * @param type
     */
    @Override
    public void onNestedPreScroll(@NonNull CoordinatorLayout coordinatorLayout, @NonNull View child, @NonNull View target, int dx, int dy, @NonNull int[] consumed, int type) {
        super.onNestedPreScroll(coordinatorLayout, child, target, dx, dy, consumed, type);
        int scrollY = target.getScrollY();
        child.setScrollY(scrollY);
    }

    /**
     * 手指快速滑动时调用的方法
     * @param coordinatorLayout
     * @param child
     * @param target
     * @param velocityX
     * @param velocityY
     * @param consumed
     * @return
     */
    @Override
    public boolean onNestedFling(@NonNull CoordinatorLayout coordinatorLayout, @NonNull View child, @NonNull View target, float velocityX, float velocityY, boolean consumed) {
        if(child instanceof  NestedScrollView){
            ((NestedScrollView) child).fling((int)velocityY);
        }
        return true;
    }
}
